package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.control.AddressControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AddressData.Address;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshScrollView;
import com.dongdongsongcan.app.view.AddressItem;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.DpTitleBar;
import com.su.dolphin.view.activity.BaseActivity;

import java.util.Collections;
import java.util.List;

public class AddressListActivity extends BaseActivity {
    private LinearLayout mArealist_ll;
    private int mMode;
    public static final int MODE_CHOOSE = 0;
    public static final int MODE_EDIT = 1;
    public PullToRefreshScrollView mPullToRefreshScrollView;

    public static void startFrom(Context context, int mode) {
        Bundle bundle = new Bundle();
        bundle.putInt("mode", mode);
        DpActivityManager.i().startActivity(context,
                AddressListActivity.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMode = this.getIntent().getExtras().getInt("mode");
        setContentView(R.layout.activity_address_list);
        getDpTitleBar().setTitle("送货地址列表");
        getDpTitleBar().setRightButtonText("新增地址", new DpTitleBar.OnRightButtonClickListener() {
            @Override
            public void onRightBtnClick(View button) {
                AddressEditActivity.startFrom(AddressListActivity.this, null);
            }
        }, null, null);

    }

    @Override
    public void initView() {
        mPullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.arealist_ptr_scrollview);
        mArealist_ll = (LinearLayout) findViewById(R.id.favourate_list);
    }

    @Override
    public void setListener() {
        mPullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getAddresslist();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppThread.uiPost(new Runnable() {
            @Override
            public void run() {
                getAddresslist();
            }
        }, 500);
    }

    private void getAddresslist() {
        AddressControl.i().user_address_list(
                new IControlResponse<AddressData>() {

                    @Override
                    public void onControlResponse(AddressData data) {
                        mArealist_ll.removeAllViews();
                        //TipsUtil.toast(data.getMsg());
						final List<Address> addresses = data.getResult();
                        Collections.reverse(addresses);
                        for (int i = 0; i < addresses.size(); i++) {
                            AddressItem addressItemView = new AddressItem(
                                    AddressListActivity.this);
                            final Address address = addresses.get(i);
                            addressItemView.setInfo(addresses.get(i));
                            mArealist_ll.addView(addressItemView.getView());
                            final int finalI = i;
                            addressItemView.getView().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mMode == MODE_CHOOSE) {
                                        UserControl.i().saveDefaultAddress(address);
                                        finish();
                                    }
                                    if (mMode == MODE_EDIT) {
												AddressEditActivity
														.startFrom(
																AddressListActivity.this,
																addresses.get(finalI));
                                    }
                                }
                            });
                        }
                        mPullToRefreshScrollView.onRefreshComplete();
                    }
                });
    }

    @Override
    public void initData() {

    }

}
